home *** CD-ROM | disk | FTP | other *** search
/ Windows News 2010 Summer - Disc 1 / WN_Ete2010_CD1.iso / Onglet5 / Weezo / Weezo setup.exe / {code_appDir} / www / local / logOutputs / ftp.php < prev    next >
PHP Script  |  2010-05-19  |  5KB  |  143 lines

  1. <?php
  2. /**
  3.  * Update an HTML page on a personal website throuht FTP
  4.  * Asynchronously called by cfLogEvent not to block calling script if this script takes time to respond
  5.  * Basic presentation:
  6.  * - Retreive HTML template located into theme directory.
  7.  *  (This template is copied from /www/local/logOutput if not existing, thus allowing user to modify template located into theme dir)
  8.  * - retreive previous events (such as server state or last connections / resources views) from /data/lastDisplayedFTPEvents.txt
  9.  *     (this file is a serialized array :
  10.  *     'serverState'=>array(timestamp,logstring)
  11.  *  'events'>array(timestamp=>logstring, timestamp=>eventType/logstring, timestamp=>logstring, ...)
  12.  *
  13.  * - rebuild page mixing new event and previous ones
  14.  * - send to FTP server
  15.  *
  16.  *
  17.  * PHP version 5
  18.  *
  19.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  20.  * that is available through the world-wide-web at the following URI:
  21.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  22.  * the PHP License and are unable to obtain it through the web, please
  23.  * send a note to license@php.net so we can mail you a copy immediately.
  24.  *
  25.  * @category   NA
  26.  * @package    NA
  27.  * @author     Nicolas Bruley / Peer 2 World <contact@weezo.net>
  28.  * @copyright  2005-2009 Nicolas Bruley / Peer 2 World
  29.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  30.  * @version    CVS: $Id:$
  31.  * @link       http://www.weezo.net
  32.  * @since      File available since Release 2.0.0
  33.  */
  34.  
  35. ignore_user_abort(true);
  36.  
  37. if(!($logString=base64_decode(@$_GET['b64LogString']))) die();
  38.  
  39. // Get FTP configuration
  40. $ftpConfig=@unserialize(cfStoredPasswordDecrypt(cfGGetVar('ftpEventConfig')));
  41. if(!isset($ftpConfig['host']) || !isset($ftpConfig['user']) || !isset($ftpConfig['password']) || !isset($ftpConfig['path']) || !isset($ftpConfig['displayIP'])){
  42.     cfLog('FTP event logging aborted - incorrect FTP configuration',LOG_DEBUG);
  43.     exit;
  44. }
  45.  
  46. // Retreive template
  47. $templateFile=cfAppDocRoot().'/local/logOutputs/FTP template.html';
  48. $userTemplateFile=cfAppDocRoot().'/themes/FTP template.html';
  49. if(!file_exists($userTemplateFile)) copy($templateFile,$userTemplateFile);
  50. $html=file_get_contents($userTemplateFile);
  51.  
  52. // Retreive current displayed page data
  53. $lastDisplayedFTPEventsFile=cfAppDataDir().'/lastDisplayedFTPEvents.txt';
  54. if(file_exists($lastDisplayedFTPEventsFile)) $lastDisplayedFTPEvents=@unserialize(file_get_contents($lastDisplayedFTPEventsFile));
  55.  
  56. /**
  57.  * Server state & time
  58.  */
  59. if($_GET['eventType']==EVENT_SERVERSTATE || !isset($lastDisplayedFTPEvents['serverState'])){
  60.     $regInfo=cfIsRegistered();
  61.  
  62.     // Replace logstring as it's not correctly HTML formated
  63.     $logString=APPLICATION_NAME.' - '.cfCaption('apacheStartedWeb',false,false,false,true);
  64.     if(isset($regInfo['regName'])) $logString.='<a class="serverStateURL" href="'.DNS_SITE.'/'.$regInfo['regName'].'">'.DNS_SITE.'/'.$regInfo['regName'].'</a>';
  65.     if(@$ftpConfig['displayIP'])
  66.         $logString.=' <a class="serverStateIP" href="'.cfExternalHostName().'">('.cfCaption('genLink',false,false,false,true).')</a>';
  67.  
  68.     $lastDisplayedFTPEvents['serverState']=array(time(),$logString);
  69.     $lastDisplayedFTPEvents['events']=array(); // Reset events
  70. }
  71.  
  72.  
  73. // Time
  74. if($lastDisplayedFTPEvents['serverState'][0])
  75.     $html=str_replace('{serverTime}',date(cfCaption('_FULL_DATE_FORMAT'),$lastDisplayedFTPEvents['serverState'][0]),$html);
  76. else
  77.     $html=str_replace('{serverTime}','',$html); // (shouldn't happend)
  78.  
  79. // State
  80. $html=str_replace('{serverState}',$lastDisplayedFTPEvents['serverState'][1],$html);
  81.  
  82.  
  83. /**
  84.  * Events
  85.  */
  86. if($_GET['eventType']!=EVENT_SERVERSTATE) $lastDisplayedFTPEvents['events'][time()]=((int)@$_GET['eventType']).'/'.$logString;
  87.  
  88. // Sort
  89. krsort($lastDisplayedFTPEvents['events']);
  90.  
  91. // Generate HTML
  92. $eventsHTML='';
  93.  
  94. foreach ($lastDisplayedFTPEvents['events'] as $time=>$logString){
  95.     @list($eventType,$logString)=explode('/',$logString,2);
  96.  
  97.     if($eventType==EVENT_RESOURCEACCESS) $eventType='eventResourceAccess'; else $eventType='eventConnection';
  98.  
  99.     $eventsHTML.='<div class="event">'."\n";
  100.     $eventsHTML.='    <span class="eventTime">'.date(cfCaption('_FULL_DATE_FORMAT'),$time).'</span>'."\n";
  101.     $eventsHTML.='    <span class="'.$eventType.'">'.$logString."</span>\n";
  102.     $eventsHTML.="</div>\n";
  103. }
  104.  
  105. $html=str_replace('{events}',$eventsHTML,$html);
  106.  
  107. // Save events
  108. file_put_contents($lastDisplayedFTPEventsFile,serialize($lastDisplayedFTPEvents));
  109.  
  110. /**
  111.  * FTP send
  112.  */
  113.  
  114. // Setup FTP connection
  115. if($ftpConfig['port']=='990') $connId = ftp_ssl_connect($ftpConfig['host'],990);
  116. else $connId = ftp_connect($ftpConfig['host'],$ftpConfig['port']);
  117.  
  118. if(!$connId){
  119.     cfLog('FTP event logging aborted - could not establish FTP connection',LOG_DEBUG);
  120.     exit;
  121. }
  122.  
  123. // Login with username and password
  124. $loginResult = @ftp_login($connId, $ftpConfig['user'], $ftpConfig['password']);
  125. if(!$loginResult){
  126.     cfLog('FTP event logging aborted - could not connect with given user/password',LOG_DEBUG);
  127.     exit;
  128. }
  129.  
  130. // Put file
  131. $tmpFile=cfAppTempDir().'/tmp_FTP.html'; file_put_contents($tmpFile,$html);
  132. $result=ftp_put($connId, $ftpConfig['path'], $tmpFile, FTP_ASCII);
  133. @unlink($tmpFile);
  134.  
  135. if(!$result){
  136.     cfLog('FTP event logging failed - could not put file on server',LOG_DEBUG);
  137.     exit;
  138. }
  139.  
  140. // Close FTP connection
  141. ftp_close($connId);
  142.  
  143. //@unlink('C:/P1/www/testZ.php'); cfDbg($html,'C:/P1/www/testZ.php',false);